'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url agent/callback/pub/dy_sendtest 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------

		const crypto = require('crypto');
		const sha1 = crypto.createHash('sha1');

		let url = "https://clue.oceanengine.com/outer/wechat/applet/token/1757233229756557";
		let nonce = 8889
		let token = "82A9DF19BD811D85ECA0DB54D13DC4A6";
		let times = vk.pubfn.toDecimal(new Date() / 1000, 0);
		console.log("times=", times);

		let strList = [token, times, nonce];
		strList.sort();
		let buffer = "";
		strList.forEach(str => {
			buffer += str;
		})
		console.log("buffer=", buffer);
		let sign = sha1.update(buffer).digest('hex');
		console.log("sign=", sign);

		let reback_url = url + "?timestamp=" + times + "&nonce=" + nonce + "&signature=" + sign;
		console.log("reback_url=", reback_url);

		let dy2_result = await uniCloud.httpclient.request(reback_url, {
			method: 'POST',
			data: {
				"clue_token": "1757722816353293,1759174248170670",
				"open_id": "oLZ6m5X2cGJuE3F7Bx340f6yIFrs",
				"event_type": "2", //参考：https://bytedance.feishu.cn/docx/doxcnmjDmhXc1Vial05xpvdu9ed
				"props": {
					"pay_amount": 1 //单位：分
				}
			},
			contentType: 'json',
			dataType: 'json'
		})

		console.log("dy2_result=", dy2_result);

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
